package cn.sinata.wanglin.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.xilada.xldutils.utils.Utils;

import cn.sinata.wanglin.db.DBHelper;
import cn.sinata.wanglin.db.model.User;

/**
 *
 * Created by liaoxiang on 16/12/31.
 */

public class GroupUserDao {
    public static final String COLUMN_USER_ID = "userId";//接收者环信id。
    public static final String COLUMN_NICK = "nick";//昵称
    public static final String COLUMN_HEAD_URL= "headUrl";//头像
    private static DBHelper dbHelper;
    private GroupUserDao(){}
    public static GroupUserDao getInstance(Context context){
        dbHelper = DBHelper.getInstance(context);
        return GroupUserDao.Singleton.INSTANCE;
    }

    private static final class Singleton {
        private final static GroupUserDao INSTANCE= new GroupUserDao();
    }

    /**
     *
     * @param groupId
     * @param userId
     * @param nick
     * @param headUrl
     */
    public void saveGroupUser(String groupId,String userId,String nick,String headUrl){
        Utils.systemErr("-GroupUser->>"+groupId+"-->"+nick+"-->"+headUrl);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db != null && db.isOpen()) {
            String tableName = getTableName(groupId);
            if (!isTableExist(tableName)) {
                db.execSQL(getCreateTableSQLString(tableName));
            }
            ContentValues values = new ContentValues();
            values.put(COLUMN_USER_ID, userId);
            nick = nick==null?"":nick;
            values.put(COLUMN_NICK, nick);
            headUrl = headUrl == null ?"":headUrl;
            values.put(COLUMN_HEAD_URL, headUrl);
            Utils.systemErr("-save group user ->"+tableName+"--->"+headUrl);
            db.replace(tableName, null, values);
        }
    }



    /**
     * 获取用户群昵称和头像。
     * @param userId 环信帐号
     * @return
     */
    public User getUser(String groupId,String userId){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        if (db != null && db.isOpen()) {
            String tableName = getTableName(groupId);
            if (!isTableExist(tableName)) {
                return null;
            }
            Cursor cursor = db.rawQuery("select * from "+tableName+" where "+COLUMN_USER_ID+" = ? ",new String[]{userId});
            if (cursor.moveToFirst()) {
                String nick = cursor.getString(cursor.getColumnIndex(COLUMN_NICK));
                String headUrl = cursor.getString(cursor.getColumnIndex(COLUMN_HEAD_URL));
                User user = new User();
                user.setHeadUrl(headUrl);
                user.setName(nick);
                Utils.systemErr(user);
                cursor.close();
                return user;
            }
        }
        return null;
    }

    private boolean isTableExist(String tableName){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        if (db==null || !db.isOpen()) {
            return false;
        }
        boolean result = false;
        if (tableName == null) {
            return false;
        }
        Cursor cursor = null;
        try {
            String sql = "select count(*) as c from Sqlite_master  where type ='table' and name ='"
                    + tableName.trim() + "' ";
            cursor = db.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    result = true;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return result;
    }

    private String getCreateTableSQLString(String tableName){
        String sql=null;
        if (TextUtils.isEmpty(tableName)) {
            return sql;
        }
        sql="CREATE TABLE "
                + tableName.trim() + "  ("
                + COLUMN_USER_ID + " TEXT PRIMARY KEY UNIQUE, "
                + COLUMN_NICK + " TEXT, "
                + COLUMN_HEAD_URL + " TEXT); ";
        return sql;
    }

    private String getTableName(String groupId){
        return "group_user_"+groupId;
    }

}
